package com.wxgo.utils;

import javax.xml.parsers.ParserConfigurationException;

import com.jfinal.kit.HttpKit;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.Ret;

public class SMSUtils {

	private static Prop p = loadConfig();
	private static String SMS_URL = p.get("SMS_URL");
	private static String SMS_USER = p.get("SMS_USER");
	private static String SMS_PWD = p.get("SMS_PWD");

	private static Prop loadConfig() {
		try {
			// 优先加载生产环境配置文件
			return PropKit.use("dbInfo.properties");
		} catch (Exception e) {
			// 找不到生产环境配置文件，再去找开发环境配置文件
			return PropKit.use("dbInfo_dev.properties");
		}
	}

	public static String post(Ret ret) {
		String returnMsg = null;
		String data = smsTemplete(ret);
		HttpKit.setCharSet("GB2312");
		returnMsg = HttpKit.post(SMS_URL, data);
		System.out.println("短信接口回复=" + returnMsg);
		try {
			return SimpleXMLUtils.readSimpleXMLStr(returnMsg);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		return null;
	}

	private static String smsTemplete(Ret ret) {
		StringBuffer data = new StringBuffer(63);
		data.append("CorpID=").append(SMS_USER);
		data.append("&Pwd=").append(SMS_PWD);
		data.append("&Mobile=").append(ret.getStr("Mobile"));
		data.append("&Content=").append(ret.getStr("Content"));
		data.append("&SendTime=").append(ret.getStr("SendTime"));
		data.append("&Cell=").append(ret.getStr("Cell"));
		return data.toString();
	}

	/*
	 * private String returnMsgTemplete(String returnMsg){
	 * 
	 * }
	 */
}
